/**
 * 
 */
package com.psytech.app.task.driver.task.impl.stm;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.namespace.QName;

import taskmanager.psytech.com.taskmanagerservice._2.BusinessFault_Exception;
import taskmanager.psytech.com.taskmanagerservice._2.ObjectFactory;
import taskmanager.psytech.com.taskmanagerservice._2.PriorityType;
import taskmanager.psytech.com.taskmanagerservice._2.StatusType;
import taskmanager.psytech.com.taskmanagerservice._2.TaskMasterDataService_Service;
import taskmanager.psytech.com.taskmanagerservice._2.TechnicalFault_Exception;
import taskmanager.psytech.com.taskmanagerservice._2.Todo;
import taskmanager.psytech.com.taskmanagerservice._2.UserUidList;

import com.psytech.app.task.web_ui.Comment;
import com.psytech.app.task.web_ui.Task;
import com.psytech.app.task.web_ui.User;
import com.psytech.app.task.web_ui.Task.PRIORITY;
import com.psytech.app.task.web_ui.Task.STATUS;
import com.psytech.app.task.web_ui.driver.TaskDriverInterface;
import com.psytech.app.task.web_ui.driver.exception.InvalidCriteria;
import com.psytech.app.task.web_ui.driver.exception.ObjectNotFoundException;

/**
 * @author ben
 */
public class StmTaskDriver implements TaskDriverInterface {

	private String wsdlUri;

	private static Logger LOG = Logger.getLogger(StmTaskDriver.class.getPackage().getName());

	private final static ObjectFactory OBJECT_FACTORY = new ObjectFactory();

	private taskmanager.psytech.com.taskmanagerservice._2.TaskMasterDataService managerService;

	private taskmanager.psytech.com.taskmanagerservice._2.TaskMasterDataService getTaskManagerServiceInstance() {
		if (managerService == null)
			synchronized (StmTaskDriver.class) {
				if (managerService == null) {
					if (wsdlUri == null)
						throw new IllegalStateException("serviceUri must be set before invoking any getter");
					try {
						LOG.info("wsdlLocation " + wsdlUri);
						final TaskMasterDataService_Service managerServiceService = new TaskMasterDataService_Service(
								new URL(wsdlUri), new QName("http://com.psytech.taskmanager/TaskManagerService/2",
								"TaskMasterDataService"));
						managerService = managerServiceService.getTaskManagerServiceSOAP();
					} catch (final MalformedURLException e) {
						throw new IllegalStateException("Probléme lors de l'initialisation du service", e);
					}
				}
			}
		return managerService;
	}

	@Override
	public void setServiceUri(final String serviceUri) {
		wsdlUri = serviceUri;
	}

	@Override
	public void setUserName(final String userName) {
		// Non utilisé

	}

	@Override
	public void setPassword(final String password) {
		// Non utilisé

	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#searchTask(com.psytech.app.task.web_ui.Task)
	 */
	@Override
	public List<Task> searchTask(final Task taskCriteria) throws InvalidCriteria {
		final List<Task> tasks = new ArrayList<Task>();
		try {
			for (final taskmanager.psytech.com.taskmanagerservice._2.Task task : getTaskManagerServiceInstance().searchTask(taskToWsTask(taskCriteria)).getTaskDocument())
				tasks.add(wsTaskToTask(task));
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
		return tasks;
	}

	@Override
	public List<Task> searchTasksAssignedToUsers(final User user) throws ObjectNotFoundException,
	IllegalArgumentException {
		if (user == null)
			throw new IllegalArgumentException("The user can not be null");
		try {
			final UserUidList userUidList = OBJECT_FACTORY.createUserUidList();
			userUidList.getUserUid().add(user.getUid());
			final List<taskmanager.psytech.com.taskmanagerservice._2.Task> wsTasks = getTaskManagerServiceInstance().searchTasksAssignedToUsers(userUidList).getTaskDocument();
			final List<Task> tasks = new ArrayList<Task>();
			for (final taskmanager.psytech.com.taskmanagerservice._2.Task task : wsTasks)
				tasks.add(wsTaskToTask(task));
			return tasks;
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public List<Task> searchNotAssignedTasks(final Task taskCriteria) throws ObjectNotFoundException,
	IllegalArgumentException {
		if (taskCriteria == null)
			throw new IllegalArgumentException("The task criteria can not be null");
		try {
			final taskmanager.psytech.com.taskmanagerservice._2.Task wsTask = taskToWsTask(taskCriteria);
			wsTask.setAssignedUsersUids(OBJECT_FACTORY.createTaskAssignedUsersUids(OBJECT_FACTORY.createUserUidList()));
			wsTask.getAssignedUsersUids().setNil(true);
			List<taskmanager.psytech.com.taskmanagerservice._2.Task> wsTasks;
			wsTasks = getTaskManagerServiceInstance().searchTask(wsTask).getTaskDocument();
			final List<Task> tasks = new ArrayList<Task>();
			for (final taskmanager.psytech.com.taskmanagerservice._2.Task task : wsTasks)
				tasks.add(wsTaskToTask(task));
			return tasks;
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage(), exception);
		}
	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#removeTask(java.lang.String)
	 */
	@Override
	public void deleteTask(final Task task) throws ObjectNotFoundException {
		if (task == null)
			throw new IllegalArgumentException("task can not be null");
		else if (task.getUid() == null)
			throw new IllegalArgumentException("taskUid can not be null");

		try {
			final taskmanager.psytech.com.taskmanagerservice._2.Task wsTask = OBJECT_FACTORY.createTask();
			wsTask.setUid(task.getUid());
			getTaskManagerServiceInstance().deleteTask(wsTask);
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage(), exception);
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage(), exception);
		}

	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#getCommentsByTask(java.lang.String)
	 */
	@Override
	public List<Comment> getCommentsByTask(final Task task) throws ObjectNotFoundException, IllegalArgumentException {
		if (task == null)
			throw new IllegalArgumentException("task can not be null");
		else if (task.getUid() == null)
			throw new IllegalArgumentException("taskUid can not be null");
		final taskmanager.psytech.com.taskmanagerservice._2.Task wsTask = OBJECT_FACTORY.createTask();
		wsTask.setUid(task.getUid());
		final List<Comment> comments = new ArrayList<Comment>();
		try {
			for (final taskmanager.psytech.com.taskmanagerservice._2.Comment comment : getTaskManagerServiceInstance().getCommentsByTask(wsTask).getCommentDocument())
				comments.add(wsCommentToComment(comment));

		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage(), exception);
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage(), exception);
		} catch (final Exception e) {
			LOG.log(Level.WARNING, "Error on comment fetching", e);
			throw new RuntimeException();
		}

		return comments;
	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#insertComment(com.psytech.app.task.web_ui.Comment)
	 */
	@Override
	public Comment createComment(final Comment comment) throws IllegalArgumentException, ObjectNotFoundException {
		final taskmanager.psytech.com.taskmanagerservice._2.Comment commentToCreate = OBJECT_FACTORY.createComment();
		commentToCreate.setWriterUid(comment.getWriterUid());
		commentToCreate.setText(comment.getText());
		final taskmanager.psytech.com.taskmanagerservice._2.Task task = OBJECT_FACTORY.createTask();
		task.setUid(comment.getTaskUid());
		commentToCreate.setTask(task);
		try {
			final taskmanager.psytech.com.taskmanagerservice._2.Comment createdComment  = getTaskManagerServiceInstance().createComment(commentToCreate);
			comment.setUid(createdComment.getUid());
			return comment;
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		} catch (final Exception e) {
			LOG.log(Level.SEVERE, "Error on comment management", e);
			throw new RuntimeException();
		}
	}

	/* (non-Javadoc)
	 * @see com.psytech.app.task.web_ui.driver.TaskDriverInterface#removeComment(java.lang.String)
	 */
	@Override
	public void deleteComment(final Comment comment) throws ObjectNotFoundException, IllegalArgumentException {
		if (comment == null)
			throw new IllegalArgumentException("comment can not be null");
		else if (comment.getUid() == null)
			throw new IllegalArgumentException("comment uid con not be null");
		try {
			final taskmanager.psytech.com.taskmanagerservice._2.Comment wsComment = OBJECT_FACTORY.createComment();
			wsComment.setUid(comment.getUid());
			getTaskManagerServiceInstance().deleteComment(wsComment);
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	private Task wsTaskToTask(final taskmanager.psytech.com.taskmanagerservice._2.Task wsTask) {
		assert wsTask != null;
		final Task tmpTask = new Task();
		tmpTask.setUid(wsTask.getUid());
		tmpTask.setName(wsTask.getName());
		if ((wsTask.getDescription() != null) && !wsTask.getDescription().isNil())
			tmpTask.setDescription(wsTask.getDescription().getValue());
		tmpTask.setPriority(PRIORITY.valueOf(wsTask.getPriority().toString()));
		tmpTask.setStatus(STATUS.valueOf(wsTask.getStatus().toString()));
		if (wsTask.getExpectedEndDate() != null)
			tmpTask.setExpectedEndDate(wsTask.getExpectedEndDate().getValue().toGregorianCalendar().getTime());
		if (wsTask.getExpectedStartDate() != null)
			tmpTask.setExpectedStartDate(wsTask.getExpectedStartDate().getValue().toGregorianCalendar().getTime());
		tmpTask.setCreatorUid(wsTask.getCreatorUid());
		if ((wsTask.getAssignedUsersUids() != null)&& !wsTask.getAssignedUsersUids().isNil() && (wsTask.getAssignedUsersUids().getValue().getUserUid().size() > 0))
			tmpTask.setAssignedUserUid(wsTask.getAssignedUsersUids().getValue().getUserUid().get(0));
		assert wsTask.getUid() != null;
		return tmpTask;
	}

	private taskmanager.psytech.com.taskmanagerservice._2.Task taskToWsTask(final Task task) {
		assert task != null;
		final taskmanager.psytech.com.taskmanagerservice._2.Task wsTask = OBJECT_FACTORY.createTask();
		wsTask.setUid(task.getUid());
		wsTask.setName(task.getName());
		if (task.getDescription() != null)
			wsTask.setDescription(OBJECT_FACTORY.createTaskDescription(task.getDescription()));
		if (task.getPriority() != null)
			wsTask.setPriority(PriorityType.fromValue(task.getPriority().toString()));
		if (task.getStatus() != null)
			wsTask.setStatus(StatusType.fromValue(task.getStatus().toString()));
		if (task.getExpectedEndDate() != null)
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedEndDate());
				wsTask.setExpectedEndDate(OBJECT_FACTORY.createTaskExpectedEndDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Problème de conversion de date", exception);
			}
			if (task.getExpectedStartDate() != null)
				try {
					final GregorianCalendar calendar = new GregorianCalendar();
					calendar.setTime(task.getExpectedStartDate());
					wsTask.setExpectedStartDate(OBJECT_FACTORY.createTaskExpectedStartDate(DatatypeFactory.newInstance()
							.newXMLGregorianCalendar(calendar)));
				} catch (final DatatypeConfigurationException exception) {
					LOG.log(Level.WARNING, "Problème de conversion de date", exception);
				}
				if (task.getCreatorUid() != null)
					wsTask.setCreatorUid(task.getCreatorUid());
				if (task.getAssignedUserUid() != null){
					wsTask.setAssignedUsersUids(OBJECT_FACTORY.createTaskAssignedUsersUids(OBJECT_FACTORY.createUserUidList()));
					wsTask.getAssignedUsersUids().getValue().getUserUid().add(task.getAssignedUserUid());
				}
				return wsTask;
	}

	private Comment wsCommentToComment(final taskmanager.psytech.com.taskmanagerservice._2.Comment wsComment) {
		assert wsComment != null;
		final Comment comment = new Comment();
		comment.setUid(wsComment.getUid());
		comment.setText(wsComment.getText());
		comment.setWriterUid(wsComment.getWriterUid());
		comment.setCreationDate(wsComment.getCreationDate().toGregorianCalendar().getTime());
		comment.setTaskUid(wsComment.getTask().getUid());
		return comment;

	}

	@Override
	public Task assignsTaskToUser(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		if (task.getAssignedUserUid() != null) {
			clearTaskAssignments(task);
			final Todo todo = OBJECT_FACTORY.createTodo();
			todo.setTask(existingTask);
			final UserUidList userUidList = OBJECT_FACTORY.createUserUidList();
			userUidList.getUserUid().add(task.getAssignedUserUid());
			todo.setUserToAffect(userUidList);
			try {
				return wsTaskToTask(getTaskManagerServiceInstance().assignTaskToUsers(todo));
			}catch (final BusinessFault_Exception exception) {
				LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
				throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
			} catch (final TechnicalFault_Exception exception) {
				LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
				throw new IllegalStateException(exception.getFaultInfo().getMessage());
			}
		}
		return task;
	}

	@Override
	public void clearTaskAssignments(final Task task) throws ObjectNotFoundException {
		// TODO Auto-generated method stub
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		if((existingTask.getAssignedUsersUids()!= null) && !existingTask.getAssignedUsersUids().isNil()){
			final Todo todo = OBJECT_FACTORY.createTodo();
			todo.setTask(existingTask);
			todo.setUserToAffect(existingTask.getAssignedUsersUids().getValue());
			try {
				managerService.unassignTaskToUsers(todo);
			} catch (final TechnicalFault_Exception exception) {
				LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
				throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
			} catch (final BusinessFault_Exception exception) {
				LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
				throw new IllegalStateException(exception.getFaultInfo().getMessage());
			}
		}
	}

	@Override
	public Task beginTask(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		try {
			return wsTaskToTask(managerService.startTask(existingTask));
		}catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage(), exception);
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task createTask(final Task task) throws IllegalArgumentException {
		if (task == null)
			throw new IllegalArgumentException("task can not be null");
		final taskmanager.psytech.com.taskmanagerservice._2.Task taskToCreate = OBJECT_FACTORY.createTask();

		taskToCreate.setUid(task.getUid());
		taskToCreate.setName(task.getName());
		taskToCreate.setDescription(OBJECT_FACTORY.createTaskDescription(task.getDescription()));
		taskToCreate.setPriority(PriorityType.fromValue(task.getPriority().toString()));
		taskToCreate.setStatus(StatusType.PENDING);
		if (task.getExpectedEndDate() != null)
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedEndDate());
				taskToCreate.setExpectedEndDate(OBJECT_FACTORY.createTaskExpectedEndDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Probléme de conversion de date", exception);
			}
			if (task.getExpectedStartDate() != null)
				try {
					final GregorianCalendar calendar = new GregorianCalendar();
					calendar.setTime(task.getExpectedStartDate());
					taskToCreate.setExpectedStartDate(OBJECT_FACTORY.createTaskExpectedStartDate(DatatypeFactory
							.newInstance().newXMLGregorianCalendar(calendar)));
				} catch (final DatatypeConfigurationException exception) {
					LOG.log(Level.WARNING, "Probléme de conversion de date", exception);
				}
				taskToCreate.setCreatorUid(task.getCreatorUid());

				try {
					return wsTaskToTask(managerService.createTask(taskToCreate));
				} catch (final BusinessFault_Exception exception) {
					LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
					throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
				} catch (final TechnicalFault_Exception exception) {
					LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
					throw new IllegalStateException(exception.getFaultInfo().getMessage());
				}
	}

	@Override
	public Task completeTask(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		try {
			return wsTaskToTask(managerService.completeTask(existingTask));
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task suspendTask(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		try {
			return wsTaskToTask(managerService.suspendTask(existingTask));
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task updateExpectedEndDate(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		if (task.getExpectedEndDate() != null)
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedEndDate());
				existingTask.setExpectedEndDate(OBJECT_FACTORY.createTaskExpectedEndDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Probléme de conversion de date", exception);
			}
			else
				existingTask.setExpectedEndDate(OBJECT_FACTORY.createTaskExpectedEndDate(null));

		try {
			return wsTaskToTask(managerService.updateExpectedEndDateTask(existingTask));
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task updateExpectedStartDate(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		if (task.getExpectedStartDate() != null)
			try {
				final GregorianCalendar calendar = new GregorianCalendar();
				calendar.setTime(task.getExpectedStartDate());
				existingTask.setExpectedStartDate(OBJECT_FACTORY.createTaskExpectedStartDate(DatatypeFactory
						.newInstance().newXMLGregorianCalendar(calendar)));
			} catch (final DatatypeConfigurationException exception) {
				LOG.log(Level.WARNING, "Probléme de conversion de date", exception);
			}
			else
				existingTask.setExpectedStartDate(OBJECT_FACTORY.createTaskExpectedStartDate(null));

		try {
			return wsTaskToTask(managerService.updateExpectedStartDateTask(existingTask));
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task updateTaskDescription(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		existingTask.setDescription(OBJECT_FACTORY.createTaskDescription(task.getDescription()));
		try {
			return wsTaskToTask(managerService.updateTaskDescription(existingTask));
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task updateTaskName(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		LOG.log(Level.FINE, task.getName());
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		existingTask.setName(task.getName());
		try {
			return wsTaskToTask(managerService.updateTaskName(existingTask));
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task increasePriority(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		try {
			return wsTaskToTask(managerService.increasePriority(existingTask));
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task lowPriority(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		try {
			return wsTaskToTask(managerService.lowPriority(existingTask));
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	@Override
	public Task unSpecifiPriority(final Task task) throws IllegalArgumentException, ObjectNotFoundException {
		checkIfTaskUidIsPresent(task);
		final taskmanager.psytech.com.taskmanagerservice._2.Task existingTask = getTaskByUid(task.getUid());
		try {
			return wsTaskToTask(managerService.unspecifiPriority(existingTask));
		} catch (final BusinessFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalArgumentException(exception.getFaultInfo().getMessage());
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}

	private void checkIfTaskUidIsPresent(final Task task) {
		if (task == null)
			throw new IllegalArgumentException("task can not be null");
		if (task.getUid() == null)
			throw new IllegalArgumentException("task uid can not be null");
	}

	private taskmanager.psytech.com.taskmanagerservice._2.Task getTaskByUid(final String uid)
	throws ObjectNotFoundException {
		assert uid != null;
		LOG.log(Level.FINE, uid);
		try {
			final taskmanager.psytech.com.taskmanagerservice._2.Task wsTask = OBJECT_FACTORY.createTask();
			wsTask.setUid(uid);
			final List<taskmanager.psytech.com.taskmanagerservice._2.Task> wsTasks = getTaskManagerServiceInstance().searchTask(wsTask).getTaskDocument();
			if ((wsTasks == null) || (wsTasks.size() < 1)) {
				LOG.log(Level.FINE, "No task found");
				throw new ObjectNotFoundException();
			}
			return wsTasks.get(0);
		} catch (final TechnicalFault_Exception exception) {
			LOG.log(Level.WARNING, exception.getFaultInfo().getMessage());
			throw new IllegalStateException(exception.getFaultInfo().getMessage());
		}
	}



}
